﻿using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Xna.Framework;

using XRpgLibrary.ScriptClasses;
using XRpgLibrary.CharacterClasses;
using XRpgLibrary.TileEngine;

namespace XRpgLibrary.Scripts.Movement
{
    public class Wander : GameScript
    {
        private static Random rand;
        private double pathTime;
        private Vector2 direction;
        private double traveledTime;
        private float pauseTime;
        private float waitedTime;
        private bool isPaused;

        public Wander(double pathTime = .5, float pauseTime = 3)
        {
            this.pathTime = pathTime;
            this.traveledTime = -1;
            this.pauseTime = pauseTime;
            this.waitedTime = 0;
            
            rand = new Random();
        }
            

        public override void Execute(Character self, object trigger, ScriptContext context, GameTime gameTime)
        {
            if (!isPaused && (traveledTime > pathTime || traveledTime < 0))
            {
                traveledTime = 0;
                direction = new Vector2(
                    rand.Next(-1, 2),
                    rand.Next(-1, 2));
                isPaused = true;
            }
            else if (isPaused)
            {
                if (waitedTime > pauseTime)
                {
                    isPaused = false;
                    waitedTime = 0;
                }
                else
                {
                    waitedTime += (float)gameTime.TotalGameTime.TotalSeconds;
                }
            }

            if (!isPaused)
            {
                self.Move(direction, (float)gameTime.ElapsedGameTime.TotalSeconds);
                traveledTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
            }
        }
    }
}
